﻿
Partial Class WebForm_ImportVolunteer_Search
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ' Me.InitData()
        End If
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        If e.CommandName = "lknCount" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)

            Me.gv.SelectedIndex = row.RowIndex
            RaiseEvent SelectedIndexChanged(Me, New EventArgs)
        End If

    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If TypeOf DataBinder.Eval(e.Row.DataItem, "HaveAccess") Is DBNull Then
                e.Row.Enabled = False
            ElseIf DataBinder.Eval(e.Row.DataItem, "HaveAccess") = "Y" Then
                e.Row.Enabled = True
            Else
                e.Row.Enabled = False
            End If
        End If
    End Sub

    Protected Sub gv_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.SelectedIndexChanged
        RaiseEvent SelectedIndexChanged(Me, New EventArgs)
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub
End Class

Partial Class WebForm_ImportVolunteer_Search
    'Public Sub InitData()
    '    Me.BindGV()
    'End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyUserDataFilter(True)
        Dim dt As Data.DataTable

        sql.AppendFormat("select a.ImportID,a.CreateBy,a.CreateDate, count = isnull(count,0),u.UserName, case when isnull(iu.OrganizationName,'') <> '' then iu.OrganizationName else oom.VOrganizationName end as OrganizationName, u.UserID from ImportVolunteerHeader a ")
        sql.AppendFormat("inner join (select ImportID, count(*) as count from ImportVolunteerRecord Group BY ImportID ) b ")
        sql.AppendFormat(" on a.ImportID=b.ImportID ")
        sql.AppendFormat("inner join [User] u on a.CreateBy = u.UserID ")
        sql.AppendFormat("left join InternalUser iu on u.UserID = iu.UserID ")
        sql.AppendFormat("left join (select oom.UserID, o.VOrganizationName from OtherOrganizationMapping oom ")
        sql.AppendFormat("inner join VOrganizationView o on oom.UOrganizationID = o.UOrganizationID ) oom on u.UserID = oom.UserID ")
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat("and year(a.CreateDate)='{0}' ", AnnualControl1.Anaual)
        sql.AppendFormat("and month(a.CreateDate)='{0}' ", MonthlyControl1.SelectedValue)
        sql.AppendFormat(" order by a.CreateDate desc")

        'MyObj.WriteLog(sql.ToString())

        dt = dat.GetData(sql)

        Me.gv.DataSource = dt
        Me.gv.DataBind()

        RaiseEvent DataBound(Me, New EventArgs)
        Me.gv.SelectedIndex = -1
    End Sub

    Public WriteOnly Property SelectedIndex() As Integer
        Set(ByVal value As Integer)

            If Me.gv.Rows.Count > 0 Then
                Me.gv.SelectedIndex = value
                RaiseEvent SelectedIndexChanged(Me, New EventArgs)
            End If

        End Set
    End Property

    Public ReadOnly Property SelectedImportID() As String
        Get
            If Me.gv.SelectedRow Is Nothing Then
                Return Me.gv.DataKeys(0)("ImportID")
            Else
                Return Me.gv.DataKeys(Me.gv.SelectedRow.RowIndex)("ImportID")
            End If
        End Get
    End Property

    Public ReadOnly Property Count() As Integer
        Get
            Return Me.gv.Rows.Count
        End Get
    End Property

    Public Event SelectedIndexChanged(ByVal source As Object, ByVal e As EventArgs)
    Public Event DataBound(ByVal source As Object, ByVal e As EventArgs)
End Class